package com.front;

import com.admin.service.ApplyService;
import com.admin.entity.Apply;
import com.admin.sys.utils.gen.GenUtils;
import com.admin.sys.utils.jwt.JwtUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import com.admin.sys.utils.admin.StringUtils;
import javax.servlet.http.HttpServletRequest;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
*【预约信息】管理
*/
@RestController
@RequestMapping("/api/apply")
public class ApiApplyController extends BaseController {

    @Autowired
    private ApplyService applyService;

    /**
     *【预约信息】获取分页数据
     */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Apply> page, Apply apply) {
        apply.setRegisterId(JwtUtils.getCurrentUserId());
        IPage<Apply> iPage = applyService.getPage(page, apply);
        return ResultInfo.ok("获取分页成功",iPage);
    }

    /**
    * 【预约信息】获取列表数据
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Apply apply) {
        List<Apply> list = applyService.getList(apply);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    *【预约信息】根据对象数据
    */
    @RequestMapping("/get")
    public ResultInfo get(Apply apply) {
        Apply entity = applyService.get(apply);
        return ResultInfo.ok("获取成功",entity);
    }

    /**
     *【预约信息】保存或修改
     */
    @RequestMapping("/sub")
    public ResultInfo saveOrUpdate(HttpServletRequest request,Apply apply){
        try {
            if(StringUtils.isEmpty(apply.getId())){
                apply.setApplyCode(GenUtils.getRuleNo("",5));
                apply.setRegisterId(JwtUtils.getCurrentUserId());
                apply.setApplyStatus(1);
                //防止重复预约查询
                Apply params = new Apply();
                params.setOfficeId(apply.getOfficeId());
                params.setUserId(apply.getUserId());
                params.setRegisterId(apply.getRegisterId());
                params.setApplyStatus(1);
                params.setApplyDate(apply.getApplyDate());
                List<Apply> list = applyService.getList(params);
                if(list.size()>0){
                    return ResultInfo.error("你当天已经预约过该医生了，无需重复预约！");
                }
                applyService.insert(apply);//新增
                return ResultInfo.ok("保存成功！");
            }else{
                applyService.update(apply);//修改
                return ResultInfo.ok("修改成功！");
            }
        } catch (Exception e) {
            return ResultInfo.error("保存失败！失败信息:"+e.getMessage());
        }
    }

    /**
     *【预约信息】根据id删除
     */
    @RequestMapping("/delete")
    public ResultInfo delete(HttpServletRequest request, Apply apply) {
        applyService.delete(apply.getId());
        return ResultInfo.ok("删除成功！");
    }

    /**
    * 【预约信息】批量删除
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        applyService.delAll(ids);
        return ResultInfo.ok("批量删除成功！");
    }

}



